<html lang="zh" xml:lang="zh" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	</head>
<body>
<form method="post" action="01.php?action=crawler">
	<table>
		<tr>
			<td colspan="2">
				http://www.hongfire.com分类通用采集器
			</td>
		</tr>
		<tr>
			<td width="20%">
				分类地址:
			</td>
			<td>
				<input type="text" name="url" value="<?php echo $_POST['url'];?>" />
			</td>
		</tr>
		<tr>
			<td width="20%">
				文件夹:
			</td>
			<td>
				<input type="text" name="dir" value="<?php echo $_POST['dir'];?>" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="开始采集" />
			</td>
		</tr>
	</table>
</form>
</body>
</html>

<?php
	header("Content-Type: text/html; charset=utf-8");
	function get_img_and_name($content){
		$match_str = "/<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"tddetails\">(.*)<\/table>/isU";
		preg_match_all ($match_str,$content,$array_item);
		$table_strs = $array_item[1][0];
		$match_str = "/<img width=\"(\d+)\" height=\"(\d+)\" src=\"(.*)\" border=\"0\" alt=\"(.*)\" \/>/isU";
		preg_match_all ($match_str,$table_strs,$array_item);
		//获取图片
		$img_url = $array_item[3][0];
		$img_name = $array_item[4][0];
		$img_content = array($img_url,$img_name);
		return $img_content;
	}
	function save_pic($pic_url,$local_dir){
		$filename = basename($pic_url);
		$local_name = $local_dir . $filename;
		if (!file_exists($local_name)) {
			copy($pic_url,$local_name);
		}
		else 
			return ;
	}
	function get_next_page($content){
		$match_str = "/Slide Show<\/a> &middot; <a href=\"(.*)\">Next image<\/a>/isU";
		preg_match_all ($match_str,$content,$array_item);
		$next_page = $array_item[1][0];
		return $next_page;
	}
	
	if ($_GET['action'] == 'crawler') {
		set_time_limit(0);
		$url = $_POST['url'];
		$dir = $_POST['dir'];
		if (!is_dir($dir) && !file_exists($dir)) {
			$result = mkdir($dir,0777);
			if (!$result) {
				exit();
			}
		}
		if ($url == '') {
			echo '地址不能为空';
			exit();
		}
		$content = @file_get_contents($url);
		if (!$content) {
			echo '无法打开该网页';
			exit();
		}
		
		//$base_url = 'http://www.hongfire.com/cg/showphoto.php/photo/59744/size/big/cat/5';
		//------------------------
		//$url = 'http://www.hongfire.com/cg/showphoto.php/photo/59744/size/big/cat/5';
		//--------------------------------
		
		$content = file_get_contents($url);
		$next_page = get_next_page($content);
		$img_content = get_img_and_name($content);
		$img_name = $img_content[1];
		$img_url = $img_content[0];
		save_pic($img_url,'./'.$dir.'/');
		echo 'pic:<font color=red>' . $img_name . '</font>had been saved<br />';
		//如果下一页为空,就证明没有下一页了
		while ($next_page != '') {
			echo '<font color=#6688FF>Now we checking page:' . $next_page . '</font><br />';
			$content = file_get_contents($next_page);
			$next_page = get_next_page($content);
			$img_content = get_img_and_name($content);
			$img_name = $img_content[1];
			$img_url = $img_content[0];
			save_pic($img_url,'./'.$dir.'/');
			echo 'pic:<font color=red>' . $img_name . '</font>had been saved<br />';
		}
		echo 'collect done';
	}
	else {
		return ;
	}
		

?>
